const oldArrayProto = Array.prototype

const newArrayProto = Object.create(oldArrayProto)

const methods = ["push", "pop", "shift", "unshift", "reverse", "sort", "splice"]

methods.forEach(method => {
  newArrayProto[method] = function (...args) {
    /**
     * this 就是 vm.xxxx
     * ob 就是 Observer 实例
     */
    const result = oldArrayProto[method].call(this, ...args)

    let inserted
    let ob = this.__ob__
    switch (method) {
      case "push":
      case "unshift":
        inserted = args
        break
      case "splice":
        inserted = args.slice(2)
        break
      default:
        break
    }

    if (inserted) {
      ob.observeArray(inserted)
    }

    // 数组更新了，通知watcher更新，这个是针对数组本身的dep，并不是里面的值
    ob.dep.notify()
    return result
  }
})

export default newArrayProto
